package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.dao.OldmanMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcOldmanStatus;
import com.csun.cmny.provider.service.OmcOldmanStatusService;
import com.csun.cmny.util.PublicUtil;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-29 21:52
 * @Description: the class omc oldman status service impl
 */
@Service
@Transactional
public class OmcOldmanStatusServiceImpl extends BaseService<OmcOldmanStatus>
        implements OmcOldmanStatusService {

    @Resource
    private OldmanMapper oldmanMapper;

    /**
     * 标记老人正在请假中
     *
     * @param oldmanId the  oldman id
     * @return the int.
     */
    @Override
    public Integer oldmanLeaveIng(Integer oldmanId) {

        return updateOldmanStatusIng(oldmanId, Constant.LEAVE_ING,
                Constant.OLDMAN_STATUS_OPT_CODE_LEAVE);
    }



    /**
     * 标记老人请假结束
     *
     * @param oldmanId the oldman id
     * @return tht int.
     */
    @Override
    public Integer oldmanLeaveEnd(Integer oldmanId) {

        return updateOldmanStatusEnd(oldmanId);
    }

    /**
     * 标记老人正在调房调级
     *
     * @param oldmanId the oldman id
     * @return the int.
     */
    @Override
    public Integer oldmanReviseIng(Integer oldmanId) {

        return updateOldmanStatusIng(oldmanId, Constant.REVISE_ING,
                Constant.OLDMAN_STATUS_OPT_CODE_REVISE);
    }

    /**
     * 标记老人调房调级结束
     *
     * @param oldmanId the oldman id
     * @return the int.
     */
    @Override
    public Integer oldmanReviseEnd(Integer oldmanId) {

        return updateOldmanStatusEnd(oldmanId);
    }

    /**
     * 标记老人正在出院中
     *
     * @param oldmanId the oldman id
     * @return the int.
     */
    @Override
    public Integer oldmanOutIng(Integer oldmanId) {

        return updateOldmanStatusIng(oldmanId, Constant.OLD_OUT_ING,
                Constant.OLDMAN_STATUS_OPT_CODE_OLD_OUT);
    }

    /**
     * 标记老人出院完成
     *
     * @param oldmanId the oldman id
     * @return the int.
     */
    @Override
    public Integer oldmanOutEnd(Integer oldmanId) {

        return updateOldmanStatusEnd(oldmanId);
    }

    /**
     * 根据老人Id查询老人状态
     *
     * @param oldmanId the oldman id
     * @return the omc oldman status
     */
    @Override
    public OmcOldmanStatus queryByOldmanId(Integer oldmanId) {

        return selectByKey(oldmanId);
    }

    private Integer updateOldmanStatusIng(Integer oldmanId, Integer currentStatus, Integer statusCode) {

        Integer status = 0;
        boolean update = true;
        OmcOldmanStatus omcOldmanStatus = selectByKey(oldmanId);
        if (PublicUtil.isEmpty(omcOldmanStatus)) {  // 查询老人真实状态
            status = oldmanMapper.selectByPrimaryKey(oldmanId).getStatus();
            if (PublicUtil.isEmpty(status)) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031201);
            }
            omcOldmanStatus = new OmcOldmanStatus();
            omcOldmanStatus.setOldmanId(oldmanId);
            omcOldmanStatus.setLastStatus(status);
            update = false;
        } else {
            status = omcOldmanStatus.getCurrentStatus();
            omcOldmanStatus.setLastStatus(status);
        }

        switch (status) {
            case Constant.LEAVE_ING :
                throw new OmcBizException(ErrorCodeEnum.OMC10031202);
            case Constant.REVISE_ING :
                throw new OmcBizException(ErrorCodeEnum.OMC10031203);
            case Constant.OLD_OUT_ING :
                throw new OmcBizException(ErrorCodeEnum.OMC10031204);
        }

        omcOldmanStatus.setCurrentStatus(currentStatus);
        omcOldmanStatus.setStatusCode(statusCode);

        if (update) {
            return update(omcOldmanStatus);
        } else {
            return save(omcOldmanStatus);
        }
    }

    private Integer updateOldmanStatusEnd(Integer oldmanId) {

        OmcOldmanStatus omcOldmanStatus = selectByKey(oldmanId);
        if (PublicUtil.isEmpty(omcOldmanStatus)) {
            return 0;
        }

        int tmp = omcOldmanStatus.getCurrentStatus();
        omcOldmanStatus.setCurrentStatus(omcOldmanStatus.getLastStatus());
        omcOldmanStatus.setLastStatus(tmp);

        return update(omcOldmanStatus);
    }
}
